package com.lzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzy.api.feign.FileMicroServiceFeign;
import com.lzy.base.BaseInfoProperties;
import com.lzy.enums.Sex;
import com.lzy.exceptions.GraceException;
import com.lzy.grace.result.GraceJSONResult;
import com.lzy.grace.result.ResponseStatusEnum;
import com.lzy.mapper.UsersMapper;
import com.lzy.pojo.Users;
import com.lzy.pojo.bo.ModifyUserBO;
import com.lzy.pojo.vo.UsersVO;
import com.lzy.service.UsersService;
import com.lzy.utils.DesensitizationUtil;
import com.lzy.utils.JsonUtils;
import com.lzy.utils.LocalDateUtils;
import com.mysql.cj.protocol.a.result.ResultsetRowsCursor;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author lzy
 * @since 2025-08-26
 */
@Service
public class UsersServiceImpl extends BaseInfoProperties implements UsersService {

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private FileMicroServiceFeign fileMicroServiceFeign; //调用文件微服务

    @Transactional
    @Override
    public void modifyUserInfo(ModifyUserBO userBO) {

        Users pendingUser = new Users();
        // 获得用户的id
        String userId = userBO.getUserId();
        // 获取微信号
        String WechatNum = userBO.getWechatNum();

        if(StringUtils.isBlank(userId)){
            GraceException.display(ResponseStatusEnum.USER_INFO_UPDATED_ERROR);
        }

        String imageQrCodeurl = null;
        // 每修改一次微信号需要判断该微信号在一年内是否已经被修改过
        if(StringUtils.isNotBlank(WechatNum)){
           String isExist = redis.get(REDIS_USER_ALREADY_UPDATE_WECHAT_NUM + ":" + userId);
           if(StringUtils.isNotBlank(isExist)){
               GraceException.display(ResponseStatusEnum.WECHAT_NUM_ALREADY_MODIFIED_ERROR);
           }else{ //如果微信号被修改过
               // 生成二维码
               GraceJSONResult jsonResult = fileMicroServiceFeign.generateorQrCode(WechatNum,userId);
               Object data = jsonResult.getData();
               String json = JsonUtils.objectToJson(data);
               imageQrCodeurl = JsonUtils.jsonToPojo(json, String.class);
               pendingUser.setWechatNumImg(imageQrCodeurl);
           }
        }

        pendingUser.setId(userId);
        pendingUser.setUpdatedTime(LocalDateTime.now());
        BeanUtils.copyProperties(userBO,pendingUser);

        usersMapper.updateById(pendingUser);

        // 如果用户修改微信号，则只能修改一次，放入redis中进行判断
        if(StringUtils.isNotBlank(WechatNum)){
            redis.setByDays(REDIS_USER_ALREADY_UPDATE_WECHAT_NUM + ":" + userId,userId,365);
        }
    }

    @Override
    public Users getById(String userId) {
        return usersMapper.selectById(userId);
    }

    @Override
    public Users getByWechatNumOrMobile(String queryString) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>()
                .eq("wechat_num",queryString)
                .or()
                .eq("mobile",queryString);
        return usersMapper.selectOne(queryWrapper);
    }
}
